// LogWindow.cpp : implementation file
//

#include "stdafx.h"
#include "ExamServer.h"
#include "LogWindow.h"
#include "ExamServerDlg.h"

#include "Core\RedirectedIO.h"

#include "Poco\DateTime.h"
#include "Poco\DateTimeFormatter.h"
#include "Poco\UnicodeConverter.h"
#include "Poco\Timezone.h"

// CLogWindow dialog

IMPLEMENT_DYNAMIC(CLogWindow, CDialog)

CLogWindow::CLogWindow(CWnd* pParent /*=NULL*/)
    : CDialog(CLogWindow::IDD, pParent)
{

}

CLogWindow::~CLogWindow()
{
}

void CLogWindow::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST_LOGS, m_listLogs);
}


BEGIN_MESSAGE_MAP(CLogWindow, CDialog)
    ON_WM_SIZE()
    ON_WM_TIMER()
    ON_WM_DESTROY()
    ON_WM_SHOWWINDOW()
    ON_WM_MOUSEMOVE()
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONUP()
END_MESSAGE_MAP()


// CLogWindow message handlers

void CLogWindow::OnSize(UINT nType, int cx, int cy)
{
    CDialog::OnSize(nType, cx, cy);

    // TODO: Add your message handler code here
    if (::IsWindow(m_listLogs.GetSafeHwnd()))
        m_listLogs.MoveWindow(5, 0, cx - 10, cy - 5);
}

void CLogWindow::OnOK()
{
    // TODO: Add your specialized code here and/or call the base class

    //CDialog::OnOK();
}

void CLogWindow::OnCancel()
{
    // TODO: Add your specialized code here and/or call the base class

    //CDialog::OnCancel();
}

void CLogWindow::OnTimer(UINT_PTR nIDEvent)
{
    // TODO: Add your message handler code here and/or call default
    if (nIDEvent == 0)
    {
#ifdef _UNICODE
        std::vector<std::wstring> lines;
#else
        std::vector<std::string> lines;
#endif
        if (ExamSystem::CRedirectedIO::Instance().GetLines(lines) > 0)
        {
            m_listLogs.SetRedraw(FALSE);

            LRESULT length = m_listLogs.SendMessage(LB_GETHORIZONTALEXTENT);
            CSize   sz;
            CDC*    pDC = m_listLogs.GetDC();
            for (size_t i = 0; i < lines.size(); i++)
            {
                m_listLogs.AddString(lines[i].c_str());
                sz = pDC->GetTextExtent(lines[i].c_str());
                if (sz.cx > length)
                    length = sz.cx;

                if (m_listLogs.GetCount() > MaxLineCount)
                    m_listLogs.DeleteString(0);
            }
            m_listLogs.SendMessage(LB_SETHORIZONTALEXTENT, (WPARAM)length);

            m_listLogs.SetRedraw(TRUE);
            if (IsWindowVisible())
            {
                m_listLogs.Invalidate();
                m_listLogs.UpdateWindow();
                m_listLogs.SetCurSel(m_listLogs.GetCount() - 1);
                m_listLogs.SetCurSel(-1);
            }
        }
    }

    CDialog::OnTimer(nIDEvent);
}

BOOL CLogWindow::OnInitDialog()
{
    CDialog::OnInitDialog();

    // TODO:  Add extra initialization here

    Poco::DateTime dateTime;
    dateTime.makeLocal(Poco::Timezone::utcOffset());
    std::string str = Poco::DateTimeFormatter::format(dateTime, "-------------------- Start @ %Y-%m-%d %W %H:%M:%S --------------------");

#ifdef _UNICODE
    std::wstring wstr;
    Poco::UnicodeConverter::toUTF16(str, wstr);
    m_listLogs.AddString(wstr.c_str());
#else
    m_listLogs.AddString(str.c_str());
#endif
    // initialize the parser
    ExamSystem::CRedirectedIO::Instance().StartThread();
    SetTimer(0, 100, NULL);

    return TRUE;  // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
}


void CLogWindow::OnDestroy()
{
    CDialog::OnDestroy();

    // TODO: Add your message handler code here
    ExamSystem::CRedirectedIO::Instance().StopThread();
}

void CLogWindow::OnShowWindow(BOOL bShow, UINT nStatus)
{
    CDialog::OnShowWindow(bShow, nStatus);

    // TODO: Add your message handler code here
    if (bShow)
    {
        m_listLogs.Invalidate();
        m_listLogs.UpdateWindow();
        m_listLogs.SetCurSel(m_listLogs.GetCount() - 1);
        m_listLogs.SetCurSel(-1);
    }
}

void CLogWindow::OnMouseMove(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    //static_cast<CExamServerDlg *>(GetParentOwner())->OnMouseMove(nFlags, point);
    //ClientToScreen(&point);
    //GetParentOwner()->ScreenToClient(&point);

    CDialog::OnMouseMove(nFlags, point);
}

void CLogWindow::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    //static_cast<CExamServerDlg *>(GetParentOwner())->OnLButtonDown(nFlags, point);
    //ClientToScreen(&point);
    //GetParentOwner()->ScreenToClient(&point);

    CDialog::OnLButtonDown(nFlags, point);
}

void CLogWindow::OnLButtonUp(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    //ClientToScreen(&point);
    //GetParentOwner()->ScreenToClient(&point);
    //static_cast<CExamServerDlg *>(GetParentOwner())->OnLButtonUp(nFlags, point);

    CDialog::OnLButtonUp(nFlags, point);
}
